package com.ruoyi.develop.orderCommissionRule.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.orderCommissionRule.domain.OrderCommissionRule;
import com.ruoyi.develop.orderCommissionRule.service.IOrderCommissionRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单提成规则Controller
 * @author lmm
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/orderCommissionRule/orderCommissionRule")
public class OrderCommissionRuleController extends BaseController {
    @Autowired
    private IOrderCommissionRuleService orderCommissionRuleService;

    /**
     * 查询订单提成规则列表
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:query')")
    @GetMapping("/list")
    public TableDataInfo list(OrderCommissionRule entity) {
        startPage();
        List<OrderCommissionRule> list = orderCommissionRuleService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出订单提成规则列表
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:export')")
    @Log(title = "订单提成规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderCommissionRule entity) {
        List<OrderCommissionRule> list = orderCommissionRuleService.selectList(entity);
        ExcelUtil<OrderCommissionRule> util = new ExcelUtil<OrderCommissionRule>(OrderCommissionRule. class);
        util.exportExcel(response, list, "订单提成规则数据");
    }

    /**
     * 获取订单提成规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(orderCommissionRuleService.selectById(id));
    }

    /**
     * 新增订单提成规则
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:add')")
    @Log(title = "订单提成规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody OrderCommissionRule entity) {
        return toAjax(orderCommissionRuleService.insert(entity));
    }

    /**
     * 修改订单提成规则
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:edit')")
    @Log(title = "订单提成规则", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody OrderCommissionRule entity) {
        return toAjax(orderCommissionRuleService.update(entity));
    }

    /**
     * 删除订单提成规则
     */
    @PreAuthorize("@ss.hasPermi('develop:OrderCommissionRule:remove')")
    @Log(title = "订单提成规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(orderCommissionRuleService.deleteByIds(ids));
    }



    /**
     * 根据区域和回款天数查询订单提成规则
     */
    @GetMapping("/getByAreaAndReturnDays")
    public AjaxResult getByAreaAndReturnDays(HttpServletRequest request) {
        String area = request.getParameter("area");
        String returnDays = request.getParameter("returnDays");
        OrderCommissionRule rule =  orderCommissionRuleService.getByAreaAndReturnDays(area,returnDays);
        if (rule == null) {
            return AjaxResult.error("未找到对应的订单提成规则");
        }
        return AjaxResult.success(rule);
    }

}
